package com.yswee.framework.widget.loading;

import com.yswee.framework.R;
import com.yswee.framework.utils.ViewInject;
import com.yswee.framework.widget.base.WebView;
import com.yswee.framework.widget.base.WebView.MDownloadListener;
import com.yswee.framework.widget.base.WebView.MWebChromeClient;
import com.yswee.framework.widget.base.WebView.MWebViewClient;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.ProgressBar;

public class LoadingWebView extends LoadingLayout {

	@ViewInject
	private ProgressBar pbprogress;
	@ViewInject
	private WebView uvbasewebview;
	
	private boolean bSucc;
	private boolean hideProgressBar;
	
	public void setHideProgressBar(boolean b) {
		hideProgressBar = b;
	}
	
	public WebView getWebView() {
		return uvbasewebview;
	}
	
	@Override
	protected View getErrorView() {
		return new ErrorView(mContext);
	}
	
	@Override
	protected boolean hideContentOnAction() {
		return true;
	}
	
	@Override
	protected boolean getAutoLoading() {
		return false;
	}
	
	public LoadingWebView(Context context) {
		super(context);
	}
	
	public LoadingWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public LoadingWebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	@Override
	protected int getLayoutResId() {
		return R.layout.view_webview;
	}
	
	@Override
	protected void onBindListener() {
		super.onBindListener();
		uvbasewebview.addJavascriptInterface(this, "initproject"); 
		uvbasewebview.setWebViewClient(new MWebViewClient(uvbasewebview, (Activity) mContext) {
			@Override
			public void onReceivedError(android.webkit.WebView view,
					int errorCode, String description, String failingUrl) {
				bSucc = false;
			}
			
			@Override
			public void onPageStarted(android.webkit.WebView view, String url, Bitmap favicon) {
				if (hideProgressBar) {
					gotoLoading();
				}
				bSucc = true;
			}
			
			@Override
			public void onPageFinished(android.webkit.WebView view, String url) {
				if (hideProgressBar) {
					if (bSucc) {
						gotoSuccessful();
					} else {
						gotoError();
					}
				}
			}
			
		});
		uvbasewebview.setWebChromeClient(new MWebChromeClient(uvbasewebview, (Activity) mContext) {
			@Override
			public void onProgressChanged(android.webkit.WebView view, int newProgress) {
				if (newProgress >= 100) {
					pbprogress.setVisibility(View.GONE);
				} else {
					pbprogress.setProgress(newProgress);
					pbprogress.setVisibility(View.VISIBLE);
				}
				super.onProgressChanged(view, newProgress);
			}
		});
		uvbasewebview.setDownloadListener(new MDownloadListener(uvbasewebview, (Activity) mContext));
	}
	
	@Override
	protected void onApplyData() {
		super.onApplyData();
		uvbasewebview.setDownloadPicOnLoad(true);
		uvbasewebview.setBackgroundColor(Color.parseColor("#00000000"));
		uvbasewebview.setBackgroundResource(R.color.widget_webview_bg);
	}
	
	@Override
	public void onApplyLoadingData() {
		super.onApplyLoadingData();
		uvbasewebview.reload();
	}
	
	@JavascriptInterface
	public void goback(){
		if(mContext instanceof Activity) {
			((Activity) mContext).finish();
		}
	}

    public void destroy() {
    	uvbasewebview.destroy();
    }
	
	public void loadUrl(String url) {
		uvbasewebview.loadUrl(url);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK && uvbasewebview.canGoBack()){
			uvbasewebview.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}

